﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;

namespace metaframeworkat_upres
{
    class http
    {
        public CookieContainer Cookiejar = null;

        public void Http() // في الاخر احط void ...
        {
            Cookiejar = new CookieContainer();

        }
        public string POST(String url, String postData, CookieContainer cki)
        {
            try
            {
                //create new request
                HttpWebRequest rq1 = (HttpWebRequest)HttpWebRequest.Create(url);


                //prepare headers
                rq1.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.68 Safari/534.24";
                rq1.ContentType = "application/x-www-form-urlencoded";
                rq1.AllowAutoRedirect = false;
                rq1.UseDefaultCredentials = true;
                rq1.KeepAlive = true;
                //rq1.Referer = referer;
                //rq1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                rq1.Method = "POST";
                rq1.ContentLength = postData.Length;
                rq1.CookieContainer = new CookieContainer(); rq1.CookieContainer = cki;
                //post data in form to server
                using (StreamWriter writer = new StreamWriter(rq1.GetRequestStream(), System.Text.Encoding.Default)) { writer.Write(postData); }

                //get source code html  and cookie
                HttpWebResponse resp = (HttpWebResponse)rq1.GetResponse();
                StreamReader sr = new StreamReader(resp.GetResponseStream());
                string htmlSource = sr.ReadToEnd();
                //Cookiejar.Add(resp.Cookies);

                //close response
                sr.Close();
                resp.Close();

                //log 
                //    myloger.LogMessageToFile("POST " + url);

                //return source html                
                return htmlSource;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
                return "";
            }
        }//post methode.

        public string GET(String url, CookieContainer cki)
        {
            try
            {
                //create new request
                HttpWebRequest rq1 = (HttpWebRequest)HttpWebRequest.Create(url);
                String htmlSource = "";

                //prepare headers
                rq1.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.68 Safari/534.24";
                rq1.ContentType = "application/x-www-form-urlencoded";
                rq1.AllowAutoRedirect = false;
                rq1.UseDefaultCredentials = true;
                rq1.KeepAlive = true;
                //rq1.Referer = referer;
                //rq1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                rq1.Method = "GET";
                rq1.CookieContainer = new CookieContainer(); rq1.CookieContainer = cki;

                //get source code html  and cookie
                HttpWebResponse resp = (HttpWebResponse)rq1.GetResponse();
                StreamReader sr = new StreamReader(resp.GetResponseStream());
                htmlSource = sr.ReadToEnd();
                Cookiejar.Add(resp.Cookies);

                //close response
                sr.Close();
                resp.Close();

                //log
                //      myloger.LogMessageToFile("GET " +url);

                //return source html                
                return htmlSource;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
                return "";
            }
        }//get methode.

        public String readFile(String fullpath)
        {
            StreamReader vsrTemp = new StreamReader(fullpath);
            String vstrTemp = vsrTemp.ReadToEnd();
            return vstrTemp;
        }
    }
}
